<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品分类" >
        <el-cascader style="width: 800px"  size="small"
                     v-model="categorizeArr" @change="changeProductCategorize"
                     :options="$store.state.factorDict.productCategorizeDict0"
                     :props="{value:'code',label:'chineseName' ,checkStrictly: true }"
                     clearable></el-cascader>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-select v-model="queryParams.productId" placeholder="请选择产品" clearable size="small">
          <el-option v-for="dict in productInfoOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品批次" prop="productBatch">
        <el-input  v-model="queryParams.productBatch" placeholder="请输入产品批次"
                   clearable  size="small"  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属企业" prop="enterpriseId">
        <el-cascader  v-model="enterpriseIdArr"  size="small"
                      :options="$store.state.factorDict.enterpriseDict" :show-all-levels="false"
                      :props="{value:'id',label:'enterpriseName',children:'children',checkStrictly:true,}"
                      clearable></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carbon:emissionPathLog:remove']"
        >删除</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="emissionPathLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="门类" align="center" prop="level0" />
      <el-table-column label="大类" align="center" prop="level1" />
      <el-table-column label="中类" align="center" prop="level2" />
      <el-table-column label="小类" align="center" prop="level3" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品库" align="center" prop="productId" />
      <el-table-column label="所属企业" align="center" prop="enterpriseId" />
      <el-table-column label="产品批次" align="center" prop="productBatch" />
      <el-table-column label="因子分类" align="center" prop="categorizeCode" />
      <el-table-column label="1上游排放  2下游排放" align="center" prop="factorType" />
      <el-table-column label="系数" align="center" prop="factorNum" />
      <el-table-column label="系数" align="center" prop="oldFactorNum" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改减排路径记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门类" prop="level0">
          <el-input v-model="form.level0" placeholder="请输入门类" />
        </el-form-item>
        <el-form-item label="大类" prop="level1">
          <el-input v-model="form.level1" placeholder="请输入大类" />
        </el-form-item>
        <el-form-item label="中类" prop="level2">
          <el-input v-model="form.level2" placeholder="请输入中类" />
        </el-form-item>
        <el-form-item label="小类" prop="level3">
          <el-input v-model="form.level3" placeholder="请输入小类" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品库" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品库" />
        </el-form-item>
        <el-form-item label="所属企业" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入所属企业" />
        </el-form-item>
        <el-form-item label="产品批次" prop="productBatch">
          <el-input v-model="form.productBatch" placeholder="请输入产品批次" />
        </el-form-item>
        <el-form-item label="因子分类" prop="categorizeCode">
          <el-input v-model="form.categorizeCode" placeholder="请输入因子分类" />
        </el-form-item>
        <el-form-item label="1上游排放  2下游排放" prop="factorType">
          <el-select v-model="form.factorType" placeholder="请选择1上游排放  2下游排放">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="系数" prop="factorNum">
          <el-input v-model="form.factorNum" placeholder="请输入系数" />
        </el-form-item>
        <el-form-item label="系数" prop="oldFactorNum">
          <el-input v-model="form.oldFactorNum" placeholder="请输入系数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmissionPathLog, getEmissionPathLog, delEmissionPathLog, addEmissionPathLog, updateEmissionPathLog, exportEmissionPathLog } from "@/api/carbon/emissionPathLog";
import { listProductInfo } from "@/api/factor/productInfo";
export default {
  name: "EmissionPathLog",
  data() {
    return {
      //产品库
      productInfoOptions:[],
      enterpriseIdArr:[],
      categorizeArr:[],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 减排路径记录表格数据
      emissionPathLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        level0: null,
        level1: null,
        level2: null,
        level3: null,
        productName: null,
        productId: null,
        enterpriseId: null,
        productBatch: null,
        categorizeCode: null,
        factorType: null,
        factorNum: null,
        oldFactorNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeProductCategorize(){
      let params={};
      if(this.categorizeArr.length>0){
        params.level0 = this.categorizeArr[0]
      }
      if(this.categorizeArr.length>1){
        params.level1 = this.categorizeArr[1]
      }
      if(this.categorizeArr.length>2){
        params.level2 = this.categorizeArr[2]
      }
      if(this.categorizeArr.length>3){
        params.level3 = this.categorizeArr[3]
      }
      listProductInfo(params).then(response => {
        this.productInfoOptions = response.rows;
      });
    },

    /** 查询减排路径记录列表 */
    getList() {
      if(this.enterpriseIdArr.length>0){
        this.queryParams.enterpriseId = this.enterpriseIdArr[this.enterpriseIdArr.length-1];
      }else {
        this.queryParams.enterpriseId = null;
      }
      if(this.categorizeArr.length>0){
        this.queryParams.level0 = this.categorizeArr[0]
      }else {
        this.queryParams.level0 = null;
      }
      if(this.categorizeArr.length>1){
        this.queryParams.level1 = this.categorizeArr[1]
      }else {
        this.queryParams.level1 = null;
      }
      if(this.categorizeArr.length>2){
        this.queryParams.level2 = this.categorizeArr[2]
      }else {
        this.queryParams.level2 = null;
      }
      if(this.categorizeArr.length>3){
        this.queryParams.level3 = this.categorizeArr[3]
      }else {
        this.queryParams.level3 = null;
      }
      this.loading = true;
      listEmissionPathLog(this.queryParams).then(response => {
        this.emissionPathLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        level0: null,
        level1: null,
        level2: null,
        level3: null,
        productName: null,
        productId: null,
        enterpriseId: null,
        productBatch: null,
        categorizeCode: null,
        factorType: null,
        factorNum: null,
        oldFactorNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.enterpriseIdArr=[];
      this.categorizeArr=[];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加减排路径记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEmissionPathLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改减排路径记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEmissionPathLog(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addEmissionPathLog(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEmissionPathLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
  }
};
</script>
